
import UIKit
import Eureka

//添加简历表单
class addResumeController : FormViewController {

    private let waiting=waitingAlertUtil(msg: "请稍后..")

    //传过来的resumeId
    var userId:String = String()

	override func viewDidLoad() {
        super.viewDidLoad()

        // 日期转换器
        let DateFormatter=NSDateFormatter()
        DateFormatter.dateFormat="yyyy-MM-dd"

        URLRow.defaultCellUpdate = { cell, row in cell.textField.textColor = .blueColor() }
        LabelRow.defaultCellUpdate = { cell, row in cell.detailTextLabel?.textColor = .orangeColor()  }
        CheckRow.defaultCellSetup = { cell, row in cell.tintColor = .orangeColor() }
        // 设置最大最小日期
        DateRow.defaultRowInitializer = { row in 
            row.maximumDate = DateFormatter.dateFromString("2015-12-31")
            row.minimumDate = DateFormatter.dateFromString("1950-01-01")
        }
        

        
        form +++ Section()
            <<< TextRow("resumeName"){ row in
                row.title = "简历名称"
                row.placeholder = "输入简历名称"
            }
         form +++ Section()
            <<< TextRow("userName"){ row in
                row.title = "用户名称"
                row.placeholder = "输入用户名"
            }
            <<< SegmentedRow<String>("gender") {
				$0.title = "性        别        "
        		$0.options = ["男", "女"]
                $0.cell.segmentedControl.tintColor = UIColor.hexStringToColor("#ff4081")
        	}
        	<<< SegmentedRow<String>("marriage") { 
        		$0.title = "婚        姻        "
        		$0.options = ["已婚", "未婚"]
                $0.cell.segmentedControl.tintColor = UIColor.hexStringToColor("#ff4081")
        	}
            <<< DateRow("birthday"){
                $0.title = "生        日"
                $0.value = DateFormatter.dateFromString("2010-01-01")
            }
            <<< PushRow<String>("degree"){
                $0.title = "学        历"
                $0.selectorTitle = "选择你的学历"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_DEGREE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("workyear"){
                $0.title = "工作年限"
                $0.selectorTitle = "选择你的工作年限"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_WORKYEAR)
                $0.options = array
                $0.value = array[0]
            }
            <<< PhoneRow("mobile"){
                $0.title = "手机号码"
                $0.placeholder = "输入你的手机号码"
            }
            <<< EmailRow("email"){
                $0.title = "邮        箱"
                $0.placeholder = "输入你的邮箱"
            }
            <<< PushRow<String>("policticalstatus"){
                $0.title = "政治面貌"
                $0.selectorTitle = "选择你的政治面貌"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_POLICTICAL_STATUS)
                print("测试是否超出")
                print(enumUtil.RESUME_POLICTICAL_STATUS)
                print(array)
                $0.options = array
                $0.value = array[0]
            }
             <<< TextAreaRow("seleveluation"){
                $0.title = "自我评价"
                $0.placeholder = "输入你的自我评价"
        }
        +++ Section("现居地")
            <<< PushRow<String>("province"){
                $0.title = "省份"
                $0.selectorTitle = "省份"
                let array=enumUtil.getOrderedStringArray(enumUtil.PROVINCE)
                $0.options = array
                $0.value = ""

                $0.onChange{row in
                    // 初始化城市
                    print("你更改了省份")
                    let cityRow:PushRow<String>=self.form.rowByTag("city")!
                    print(row.value)
                    if(row.value != nil){
                        let provinceID=enumUtil.getKeyByString(row.value!, dict: enumUtil.PROVINCE)
                        var littleArray=enumUtil.getOrderedStringArray(enumUtil.PROVINCE_CITYS[provinceID]!)
                        print(littleArray)
                        cityRow.options = littleArray
                        cityRow.value = littleArray[0]
                        cityRow.updateCell()
                    }
                }
            }
            <<< PushRow<String>("city"){
                $0.title = "市"
                $0.selectorTitle = "城市"
            }
        +++ Section("工作意向")
            <<< PushRow<String>("jobNature"){
                $0.title = "工作性质"
                $0.selectorTitle = "选择你的工作性质"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_WORKNATURE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("expectSalary"){
                $0.title = "薪资范围"
                $0.selectorTitle = "选择你期望的薪资范围"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_SALARY_RANGE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("expectIndustryType"){
                $0.title = "期望行业"
                $0.selectorTitle = "期望行业"
                var array=enumUtil.getOrderedStringArray(enumUtil.INDUSRTY_ENUM)
                $0.options = array
                $0.value = array[0]
            }
            +++ Section("期望职位")
            <<< PushRow<String>("jobParent"){
                $0.title = "职位大类"
                $0.selectorTitle = "职位大类"
                let array=enumUtil.getOrderedStringArray(enumUtil.JOB_FUNC_PARENT)
                $0.options = array
                $0.value = ""

                // 初始职位
                $0.onChange{row in
                    if(row.value != nil){
                        print("更改职业大类")
                        let jobRow:PushRow<String>=self.form.rowByTag("expectJob")!
                        let jobID=enumUtil.getKeyByString(row.value!, dict: enumUtil.JOB_FUNC_PARENT)
                        print(enumUtil.JOB_CHILDS)
                        print(jobID)
                        var littleArray=enumUtil.getOrderedStringArray(enumUtil.JOB_CHILDS[jobID]!)
                        jobRow.options = littleArray
                        jobRow.value = littleArray[0]
                        jobRow.updateCell()
                        
                    }
                }
            }
            <<< PushRow<String>("expectJob"){
                $0.title = "职位小类"
                $0.selectorTitle = "职位小类"
            }
            +++ Section("期望工作地点")
            <<< PushRow<String>("dream_province"){
                $0.title = "省份"
                $0.selectorTitle = "省份"
                let array=enumUtil.getOrderedStringArray(enumUtil.PROVINCE)
                $0.options = array
                $0.value = ""

                //初始化城市
                $0.onChange{row in
                    // 初始化城市
                    if(row.value != nil){
                        let cityRow:PushRow<String>=self.form.rowByTag("dream_city")!
                        let provinceID=enumUtil.getKeyByString(row.value!, dict: enumUtil.PROVINCE)
                        var littleArray=enumUtil.getOrderedStringArray(enumUtil.PROVINCE_CITYS[provinceID]!)
                        cityRow.options = littleArray
                        cityRow.value = littleArray[0]
                        cityRow.updateCell()
                    }
                }
            }
            <<< PushRow<String>("dream_city"){
                $0.title = "市"
                $0.selectorTitle = "城市"
            }
            +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = Optional("添加简历")
                    row.cell.tintColor = UIColor.hexStringToColor("#ffffff")
                    row.cell.backgroundColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.submitForm()
                })




        loadUserBasicInfo()

    }

//  加载用户基本信息
    private func loadUserBasicInfo(){

        // 等待提示
        waiting.show(self)
        
         basicInfo.getUserInfoByID(userUtil.getCurrUserId(), successCallback: { (data) in
            //            匹配信息
            self.waiting.stop()
            
//            判断用户某个信息是否填写！
            if(data != nil){
                var tr:TextRow
                var pr:PushRow<String>
                var sr:SegmentedRow<String>
                var dr:DateRow
                
//                用户填写了信息
                tr=self.form.rowByTag("userName")!
                tr.value=data["UserName"].string
                if(data["Gender"].string != nil){
                    sr=self.form.rowByTag("gender")!
                    sr.value=enumUtil.getStringByKey(Int(data["Gender"].string!)!, dict: enumUtil.RESUME_SEX)
                }
                if(data["Marrige"].string != nil){
                    sr=self.form.rowByTag("marriage")!
                    sr.value=enumUtil.getStringByKey(Int(data["Marrige"].string!)!, dict: enumUtil.RESUME_MARRIAGE)
                }
                if(data["Degree"].string != nil){
                    pr=self.form.rowByTag("degree")!
                    pr.value=enumUtil.getStringByKey(Int(data["Degree"].string!)!, dict: enumUtil.RESUME_DEGREE)
                }
                if(data["YearOfWork"].string != nil){
                    pr=self.form.rowByTag("workyear")!
                    pr.value=enumUtil.getStringByKey(Int(data["YearOfWork"].string!)!, dict: enumUtil.RESUME_WORKYEAR)
                }
                
                
                let phr:PhoneRow = self.form.rowByTag("mobile")!
                phr.value=data["Mobile"].string
                let emr:EmailRow = self.form.rowByTag("email")!
                emr.value=data["Email"].string
                
//                省市
                if(data["Location"].string != nil){
                    let cityCode:Int = Int(data["Location"].string!)!
                    let provinceCode:Int = enumUtil.getParentCodeByChildCode(cityCode)
                    let provinceName=enumUtil.getStringByKey(provinceCode, dict: enumUtil.PROVINCE)
                    let cityList = enumUtil.PROVINCE_CITYS[provinceCode]
                    let cityName = enumUtil.getStringByKey(cityCode, dict: cityList!)
                    
                    pr=self.form.rowByTag("province")!
                    pr.value=provinceName
                    pr=self.form.rowByTag("city")!
                    pr.options = enumUtil.getOrderedStringArray(cityList!)
                    pr.value = cityName
                }
               
                if(data["Birthday"].string != nil){
                    let DateFormatter=NSDateFormatter()
                    DateFormatter.dateFormat="yyyy-M-d H:mm:ss"
                    dr=self.form.rowByTag("birthday")!
                    dr.value=DateFormatter.dateFromString(data["Birthday"].string!)
                }
                
                for row in self.form.allRows{
                    row.updateCell()
                }
            }
            
            

        }) { (error) in
            self.waiting.stop()
            warningAlertUtil(title: "错误", msg: "获取用户个人信息失败").show(self)
        }

    }

    private func submitForm(){
        // 等待提示
//        waiting.show(self)
        //获取表单结果
        let dict = form.values()
        
        if(dict["province"] == nil || dict["city"] == nil || dict["dream_province"] == nil || dict["dream_city"] == nil || dict["jobParent"] == nil || dict["expectJob"] == nil){
            warningAlertUtil(title: "错误", msg: "未填写完整").show(self)
            return
        }

        //获取用户名
        let userId = userUtil.getCurrUserId()
        let resumeName = dict["resumeName"] as! String
        let userName = dict["userName"] as! String
        let gender = enumUtil.getKeyByString(dict["gender"] as! String,dict: enumUtil.RESUME_SEX)
        let marriage = enumUtil.getKeyByString(dict["marriage"] as! String,dict: enumUtil.RESUME_MARRIAGE)
        let birthday = dict["birthday"] as! NSDate
        let degree = enumUtil.getKeyByString(dict["degree"] as! String,dict: enumUtil.RESUME_DEGREE)
//        let workyear = enumUtil.getKeyByString(dict["workyear"] as! String,dict: enumUtil.RESUME_WORKYEAR)
        let workyear = enumUtil.getKeyByString(dict["workyear"] as! String,dict: enumUtil.RESUME_WORKYEAR)
        let mobile = dict["mobile"] as! String
        let email = dict["email"] as! String
        let province = enumUtil.getKeyByString(dict["province"] as! String,dict: enumUtil.PROVINCE)
        let location = enumUtil.getKeyByString(dict["city"] as! String,dict: enumUtil.PROVINCE_CITYS[province]!)
        let policticalstatus = enumUtil.getKeyByString(dict["policticalstatus"] as! String,dict: enumUtil.RESUME_POLICTICAL_STATUS)
        var seleveluation:String = ""
        if(dict["seleveluation"] != nil){
            seleveluation = dict["seleveluation"] as! String
        }
        let expectIndustryType = String( enumUtil.getKeyByString(dict["expectIndustryType"] as! String,dict: enumUtil.INDUSRTY_ENUM) )
        let jobNature = enumUtil.getKeyByString(dict["jobNature"] as! String,dict: enumUtil.RESUME_WORKNATURE)
        let expectSalary = enumUtil.getKeyByString(dict["expectSalary"] as! String,dict: enumUtil.RESUME_SALARY_RANGE)
        let expectprovince = enumUtil.getKeyByString(dict["dream_province"] as! String,dict: enumUtil.PROVINCE)
        let expectLocation = String( enumUtil.getKeyByString(dict["dream_city"] as! String,dict: enumUtil.PROVINCE_CITYS[expectprovince]!) )
        let jobParent = enumUtil.getKeyByString(dict["jobParent"] as! String,dict: enumUtil.JOB_FUNC_PARENT)
        let expectJob = String( enumUtil.getKeyByString(dict["expectJob"] as! String,dict: enumUtil.JOB_CHILDS[jobParent]!) )

        resumeManager.CreateResume(userId,
                             resumeName:resumeName,
                             userName:userName,
                             gender:gender,
                             marriage:marriage,
                             birthday:birthday,
                             degree:degree,
                             workyear:workyear,
                             mobile:mobile,
                             email:email,
                             location:location,
                             policticalstatus:policticalstatus,
                             seleveluation:seleveluation,
                             expectIndustryType:expectIndustryType,
                             expectJob:expectJob,
                             expectLocation:expectLocation,
                             jobNature:jobNature,
                             expectSalary:expectSalary,
                             successCallback: { (msg) in
                    //            添加信息ok
//                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }

}


